package com.jyxd.web.controller.zpaiban;

import com.jyxd.web.data.Zpaiban.ZoptionEntity;
import com.jyxd.web.data.Zpaiban.ZpaibanEntity;
import com.jyxd.web.data.Zpaiban.form.ZpaibanQueryForm;
import com.jyxd.web.service.zpaiban.ZoptionService;
import com.jyxd.web.service.zpaiban.ZpaibanService;
import com.alibaba.fastjson.JSONObject;
import com.jyxd.web.util.HttpCode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/zpaiban")
public class ZpaibanController {

    @Resource
    private ZpaibanService zpaibanService;

    @Resource
    private ZoptionService zoptionService;

    @PostMapping("/save")
    public String saveZpaiban(@RequestBody ZpaibanEntity zpaibanEntity) {
        JSONObject json = new JSONObject();
        try {
            if (zpaibanEntity.getId() != null){
                zpaibanService.updateById(zpaibanEntity);
            }else {
                zpaibanService.save(zpaibanEntity);

            }
        } catch (Exception e) {
            json.put("code", HttpCode.FAILURE_CODE.getCode());
            json.put("msg", "保存失败");
            json.put("data", null);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "保存成功");
        json.put("data", zpaibanEntity);
        return json.toString();
    }

    @PostMapping("/saves")
    public String saveZpaibans(@RequestBody List<ZpaibanEntity> list) {
        JSONObject json = new JSONObject();
        try {
            for (ZpaibanEntity zpaibanEntity : list) {
                if (zpaibanEntity.getId() != null){
                    zpaibanService.updateById(zpaibanEntity);
                }else {
                    zpaibanService.save(zpaibanEntity);
                }
            }
        } catch (Exception e) {
            json.put("code", HttpCode.FAILURE_CODE.getCode());
            json.put("msg", "保存失败");
            json.put("data", null);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "保存成功");
        return json.toString();
    }

    @PostMapping("/search")
    public String getZpaibanByYearAndWeekNumber(@RequestBody ZpaibanQueryForm queryParams) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");

        List<ZpaibanEntity> byYearAndWeekNumber = zpaibanService.getByYearAndWeekNumber(queryParams);

        if (byYearAndWeekNumber != null && !byYearAndWeekNumber.isEmpty()) {
            json.put("msg", "查询成功");
            json.put("data", byYearAndWeekNumber);
            json.put("code", HttpCode.OK_CODE.getCode());
        }
        return json.toString();
    }


    @PostMapping("/option/add")
    public String addOption(@RequestBody ZoptionEntity zoptionEntity) {
        JSONObject json = new JSONObject();
        try {
            if (zoptionEntity.getId() != null){
                zoptionService.updateById(zoptionEntity);
            }else {
                zoptionService.save(zoptionEntity);

            }
        } catch (Exception e) {
            json.put("code", HttpCode.FAILURE_CODE.getCode());
            json.put("msg", "保存失败");
            json.put("data", null);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "保存成功");
        json.put("data", zoptionEntity);
        return json.toString();
    }

    @GetMapping("/option/search")
    public String getOption(){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<ZoptionEntity> list = zoptionService.list();
        if (list != null && !list.isEmpty()) {
            json.put("msg", "查询成功");
            json.put("data", list);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @DeleteMapping("/option/delete/{id}")
    public String deleteOption(@PathVariable("id") Long id){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zoptionService.removeById(id);
        json.put("msg", "删除成功");
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
